//
//  SecondViewController.m
//  XDCommonApp
//
//  Created by XD-XY on 2/12/14.
//  Copyright (c) 2014 XD-XY. All rights reserved.
//

#import "SecondViewController.h"
#import "PrimaryKindCell.h"
#import "MyCollectionViewCell.h"
#import "SearchViewController.h"
@interface SecondViewController ()

@end

@implementation SecondViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.leftBtn.hidden = YES;
    self.titleLabel.text = SECONDVIEWTITLETEXT;
    
    [self initSearchViews]; //搜索框
    
    [self initLeftViews]; //左侧一级分类
    [self initRightViews]; //右侧二级分类
}

#pragma mark 创建搜索框
-(void)initSearchViews
{
    CGFloat aheight =[[[UIDevice currentDevice] systemVersion] floatValue] >= 7.f?20:0;
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,aheight, 320, 44)];
    view.backgroundColor = [UIColor clearColor];
    [self.navigationBarView addSubview:view];
    
    UIImage * searchImg = [UIImage imageNamed:@"home_04"];
    UIImage * img2 = [UIImage imageNamed:@"home_05"];
    UIButton * searchBtn = [UIButton initButton:CGRectMake(18/2.0f, 7, searchImg.size.width, searchImg.size.height) btnImage:searchImg btnTitle:nil titleColor:nil titleFont:nil];
    [searchBtn setImage:img2 forState:UIControlStateNormal];
    [searchBtn addTarget:self action:@selector(searchAction) forControlEvents:UIControlEventTouchDown];
    [view addSubview:searchBtn];
}

#pragma mark 左侧一级分类
-(void)initLeftViews
{
    self.leftTableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0, 85, self.contentView.frame.size.height-UI_TAB_BAR_HEIGHT) style:UITableViewStylePlain];
    _leftTableView.backgroundView = [UIView initViewRect:CGRectMake(0,0, 85, self.contentView.frame.size.height-UI_TAB_BAR_HEIGHT) andBackGroudImage:[UIImage imageNamed:@"typeBg_nor"]];
     _leftTableView.backgroundColor = [UIColor clearColor];
    _leftTableView.delegate = self;
    _leftTableView.dataSource = self;
    _leftTableView.showsVerticalScrollIndicator = NO;
    _leftTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.contentView addSubview:_leftTableView];
}

#pragma mark 右侧-二级分类
-(void)initRightViews
{
    UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize=CGSizeMake(90,120);
    flowLayout.sectionInset = UIEdgeInsetsMake(10,18,10,18);
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(85, 0, UI_SCREEN_WIDTH - 85, _leftTableView.frame.size.height) collectionViewLayout:flowLayout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.showsVerticalScrollIndicator = YES;
    _collectionView.backgroundColor = [UIColor whiteColor];
    [_collectionView registerClass:[MyCollectionViewCell class]
        forCellWithReuseIdentifier:@"collectionCell"];
    [self.contentView addSubview:_collectionView];
}

#pragma mark UITableView Delegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 20;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 75.0f;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellName = @"cell";
    PrimaryKindCell * cell = [tableView dequeueReusableCellWithIdentifier:cellName];
    if(cell == nil){
        cell = [[PrimaryKindCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName];
    }
    cell.contentLB.text = [NSString stringWithFormat:@"primaryKinds%d",indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

#pragma mark - UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 100;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSString * cellIde = @"collectionCell";
    MyCollectionViewCell * cell =(MyCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIde forIndexPath:indexPath];
    cell.kindName.text = [NSString stringWithFormat:@"KindName %d",indexPath.row];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"collectionView.row = %d",indexPath.row);
    
}


#pragma mark UIButton Delegate
-(void)searchAction
{
    SearchViewController * searchVC = [[SearchViewController alloc] init];
    [self.navigationController pushViewController:searchVC animated:NO];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
